//
//  ConfigViewController.swift
//  SunxianhuaTestApp
//
//  Created by abc on 2021/10/12.
//

import Foundation
import UIKit
class ConfigViewController: BaseViewController {
    
    private var collectionView : EmptyCollectionView!
    private var modelPresent :ConfigVcPresent!
    
    private var searchBar :UISearchBar!
    
    private var currentIndex :Int = 0
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.title = "搜索"
        self.modelPresent = ConfigVcPresent.init(any: self)
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.tabBarController?.tabBar.isHidden = false
    }
    
    override func setupSubViews() {
        
        
        self.searchBar = UISearchBar.init(frame: CGRect.init(x: 0, y: 0, width: 300, height: 40.0))
        searchBar.placeholder = "请输入关键字"
        searchBar.delegate = self
        self.navigationItem.titleView = searchBar
        
        // 初始化
        let layout = UICollectionViewFlowLayout.init()
        layout.scrollDirection = .vertical
        layout.sectionInset = UIEdgeInsets.init(top: 0, left: 15.0, bottom: 0, right: 15.0)
        collectionView = EmptyCollectionView.init(frame: CGRect(x:10, y:64, width:ScreenWidth-20.0, height:ScreenHeight), collectionViewLayout: layout)
        collectionView.backgroundColor = UIColor.white
        collectionView.delegate = self
        collectionView.dataSource = self
    
        self.view.addSubview(collectionView!)
        collectionView.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.bottom.equalTo(-65)
            make.top.equalToSuperview()
        }
        // 注册cell
        collectionView.register(ConfigSearchViewCell.classForCoder(), forCellWithReuseIdentifier: "cell")
    }
    
    
    private func deleteAction(index :Int){
        
        HUD.showSystemAlert(title: "删除记录", message: "确认删除搜索记录?", titleArray: [
                                "确定","取消"], alertType: .actionSheet) { index in
            
            if index == 0 {
                self.modelPresent.deleteRecode(index: index)
            }
        }
        
        
    }
    
}



extension ConfigViewController :UICollectionViewDataSource, UICollectionViewDelegate ,UICollectionViewDelegateFlowLayout{
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! ConfigSearchViewCell
        let model = self.modelPresent.listModel!.searchKeyModelArray[indexPath.row]
        cell.reloadCell(cellModel: model, isSelected: self.currentIndex == indexPath.row) {[weak self] in
            self?.deleteAction(index: indexPath.row)
        }
        return cell
    }

    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return self.modelPresent.listModel.searchKeyModelArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let width = 100.0 //self.modelPresent.listModel!.searchKeyModelArray[indexPath.row].width
        return CGSize.init(width: width, height: 50.0)
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        self.view.endEditing(true)
        self.currentIndex = indexPath.row
        self.searchBar.text = self.modelPresent.listModel.searchKeyModelArray[indexPath.row].key
        collectionView.reloadData()
        let vc = SearchListViewController.init(searchKey: self.searchBar.text ?? "")
        self.navigationController?.pushViewController(vc, animated: true)
    }
}


extension ConfigViewController :ConfigVcPresentDelegate{
    
    func reloadView() {
        
        DispatchQueue.main.async {
            self.collectionView.reloadData()
        }
    }
    
}



extension ConfigViewController :UISearchBarDelegate{
    
    func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
        
        
        self.view.endEditing(true)
        guard let key = searchBar.text else {
            return
        }
        self.modelPresent.searchKey = key
        self.modelPresent.addRecode(model: SearchKeyModel.init(key: key))
        
        self.currentIndex = self.modelPresent.listModel.searchKeyModelArray.count-1
        let vc = SearchListViewController.init(searchKey: key)
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
    
    
}
